package com.cn.yang.controller;

import com.alibaba.fastjson.JSONObject;
import com.cn.yang.bean.Group;
import com.cn.yang.service.CodeManageService;
import com.cn.yang.service.GroupService;
import com.cn.yang.service.PersonnelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.json.JsonObject;
import javax.xml.crypto.Data;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/group")
public class GroupController {
    @Autowired
    GroupService groupService;
    @Autowired
    PersonnelService personnelService;
    @Autowired
    CodeManageService codeManageService;
    @RequestMapping("/groupAdd")
    public void groupAdd(@RequestBody List<Group> list){
        personnelService.updateGroup(list);
        groupService.insertGroup(list);
    }
    @RequestMapping("/groupCodeAdd")
    public JSONObject groupCodeAdd(@RequestBody Group group){
        codeManageService.insertGroupCode(group);
        Date date = new Date();
        DateFormat bf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = bf.format(date);
        group.setCreattime(format);
        int result = groupService.groupCodeAdd(group);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        return jsonObject;
    }
    @RequestMapping("/selectGroupCode")
    public JSONObject selectGroupCode(){
        List<Group> list = groupService.selectGroupCode();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list",list);
        return jsonObject;
    }
    @RequestMapping("/selectGroup")
    public JSONObject selectGroup(@RequestBody Group group){
        String groupNumber = group.getGroupNumber();
        List<Group> list = groupService.selectGroup(groupNumber);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list",list);
        return jsonObject;
    }
    @RequestMapping("/selectGroupCodeNumber")
    public JSONObject selectGroupCodeNumber(@RequestBody Group group){
        String groupNumber = group.getGroupNumber();
        Group list = groupService.selectGroupCodeNumber(groupNumber);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list",list);
        return jsonObject;
    }
    @RequestMapping("/deleteGroup")
    public JSONObject deleteGroup(@RequestBody Group group){
        codeManageService.deleteGroup(group);
        groupService.deleteGroupCode(group);
        personnelService.updateGroupOne(group);
        int result = groupService.deleteGroup(group);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        return jsonObject;
    }
}
